package com.dream.testtubebaby.fragment;

import java.util.List;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dream.testtubebaby.data.bbs.BbsNode;
import com.dream.testtubebaby.utils.AppUtil;
import com.dream.testtubebaby.utils.NetUtil;

public class BbsGoodFragment extends BbsSubBaseFragment {
	public BbsGoodFragment() {
	}

	public BbsGoodFragment(int newsType) {
		super(newsType);
	}

	@Override
	public void onStart() {
		super.onStart();
		currentPage = 1;
	}
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return super.onCreateView(inflater, container, savedInstanceState);
	}

	@Override
	protected void loadMoreData() {
		if (isLoadingDataFromNetWork) {
			currentPage += 1;
			try {
				if (mAdapter.getCount() != 0) {
					BbsNode bbsNode = (BbsNode) mAdapter.getItem(mAdapter
							.getCount() - 1);
					if (bbsNode.getId() != 1) {
						List<BbsNode> newsItems = downLoadBbsListItem(bbsNode
								.getId());
						bbsItemDao.add(newsItems);
						mAdapter.addAll(newsItems);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			currentPage += 1;
			List<BbsNode> newsItems = bbsItemDao.list(newsType, currentPage);
			mAdapter.addAll(newsItems);
		}
	}

	@Override
	protected int refreashData() {
		if (NetUtil.checkNet(getActivity())) {
			try {
				List<BbsNode> newsItems = downLoadBbsListItem(-1);
				mAdapter.setDatas(newsItems);

				isLoadingDataFromNetWork = true;
				AppUtil.setRefreashTime(getActivity(), newsType);
				bbsItemDao.deleteAll(newsType);
				bbsItemDao.add(newsItems);
			} catch (Exception e) {
				e.printStackTrace();
				isLoadingDataFromNetWork = false;
				return TIP_ERROR_SERVER;
			}
		} else {
			isLoadingDataFromNetWork = false;
			List<BbsNode> newsItems = bbsItemDao.list(newsType, currentPage);
			mDatas = newsItems;
//			mAdapter.setDatas(newsItems);
			return TIP_ERROR_NO_NETWORK;
		}

		return -1;
	}
}
